package com.ai.bitcoin.controller;

import com.ai.bitcoin.bo.GetTemplateInfoFromEsBO;
import com.ai.bitcoin.common.resp.ResponseEntity;
import com.ai.bitcoin.constant.MonitorSettingConstrant;
import com.ai.bitcoin.enums.ResponseEnum;
import com.ai.bitcoin.jpa.dao.BccReportMonitorConfigDao;
import com.ai.bitcoin.jpa.dao.BccWeixinCoinQrcodeDao;
import com.ai.bitcoin.jpa.entity.BccReportMonitorConfig;
import com.ai.bitcoin.jpa.entity.BccWeixinCoinQrcode;
import com.ai.bitcoin.service.helper.PushTemplateEsBizHelper;
import com.ai.bitcoin.service.helper.PushWxTemplateBizHelper;
import com.ai.bitcoin.vo.BoomBustQueueVO;
import com.ai.bitcoin.vo.BoomBustVO;
import com.ai.bitcoin.vo.ErcTwoZeroVO;
import com.alibaba.fastjson.JSONObject;
import com.tianru.bitcoin.search.api.domain.BoomBust;
import com.tianru.bitcoin.search.api.domain.BuyerSellerPoint;
import com.tianru.bitcoin.search.api.domain.ErcTwoZero;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @description: ${description}
 * @author: gulj
 * @create: 2018-08-12 23:30
 **/
@RestController
@RequestMapping("/es/template")
@Slf4j
public class WxTemplateInfoGetController {


    @Autowired
    private PushTemplateEsBizHelper pushTemplateEsBizHelper;

    @Autowired
    private BccWeixinCoinQrcodeDao bccWeixinCoinQrcodeDao;

    @Autowired
    private PushWxTemplateBizHelper pushWxTemplateBizHelper;

    @Autowired
    private BccReportMonitorConfigDao monitorConfigDao;

    /**
     * 获取es中模板的信息
     *
     * @param templateInfoFromEsBO
     * @return
     */
    @PostMapping("/info")
    public ResponseEntity getTemplateInfoFromEs(@RequestBody @Valid GetTemplateInfoFromEsBO templateInfoFromEsBO) {
        String id = templateInfoFromEsBO.getId();
        switch (templateInfoFromEsBO.getType()) {
            case MonitorSettingConstrant.MONITOR_BOOM_BUST_CODE:
                BoomBust boomBustByIdFromEs = pushTemplateEsBizHelper.findBoomBustByIdFromEs(Long.valueOf(id));
                BccReportMonitorConfig monitorConfig = monitorConfigDao.findByBccIdAndOpenId(boomBustByIdFromEs.getBcc_id(), templateInfoFromEsBO.getOpenId());
                BoomBustVO boomBustVO = new BoomBustVO();
                BeanUtils.copyProperties(boomBustByIdFromEs, boomBustVO);
                if (monitorConfig != null) {
                    boomBustVO.setCoinPriceMax(monitorConfig.getCoinPriceMax());
                    boomBustVO.setCoinPriceMin(monitorConfig.getCoinPriceMin());
                    boomBustVO.setBccName(monitorConfig.getBccNameEn());
                }

                //变化幅度
                String rate = boomBustVO.getPrice_change_rate();
                if (StringUtils.isEmpty(rate)) {
                    rate = (BigDecimal.valueOf(NumberUtils.toDouble(boomBustVO.getPrice()))
                            .subtract(BigDecimal.valueOf(NumberUtils.toDouble(boomBustVO.getPrice_previous())))
                            .divide(BigDecimal.valueOf(NumberUtils.toDouble(boomBustVO.getPrice())).multiply(BigDecimal.valueOf(100)), 2, BigDecimal.ROUND_HALF_UP))
                            .toString();
                    boomBustVO.setPrice_change_rate(rate);
                }

                return new ResponseEntity(ResponseEnum.SUCCESS, boomBustVO);
            case MonitorSettingConstrant.MONITOR_BUYER_SELLER_POINT_CODE:
                BuyerSellerPoint pointByIdFromEs = pushTemplateEsBizHelper.findBuyerSellerPointByIdFromEs(Long.valueOf(id));
                return new ResponseEntity(ResponseEnum.SUCCESS, pointByIdFromEs);
            case MonitorSettingConstrant.MONITOR_TRANSFER_BIG_MONEY_CODE:
                ErcTwoZero zeroByIdFromEs = pushTemplateEsBizHelper.findErcTwoZeroByIdFromEs(id);
                ErcTwoZeroVO ercTwoZeroVO = new ErcTwoZeroVO();
                BeanUtils.copyProperties(zeroByIdFromEs, ercTwoZeroVO);
                ercTwoZeroVO.setBccName(pushWxTemplateBizHelper.getMonitorObject(zeroByIdFromEs.getBccId()));
                return new ResponseEntity(ResponseEnum.SUCCESS, ercTwoZeroVO);
            case MonitorSettingConstrant.MONITOR_PRICE_THRESHHOLD_CODE:
                BoomBust priceThreshhold = pushTemplateEsBizHelper.findBoomBustByIdFromEs(Long.valueOf(id));
                BccReportMonitorConfig config = monitorConfigDao.findByBccIdAndOpenId(priceThreshhold.getBcc_id(), templateInfoFromEsBO.getOpenId());
                BoomBustVO priceVo = new BoomBustVO();
                BeanUtils.copyProperties(priceThreshhold, priceVo);
                if (config != null) {
                    // 币的二维码链接
                    BccWeixinCoinQrcode qrcode = bccWeixinCoinQrcodeDao.findByBccId(priceThreshhold.getBcc_id());
                    if (qrcode != null) {
                        priceVo.setQrcodeUrl(qrcode.getUrl());
                    }
                    priceVo.setBcc_keyword(config.getBccNameEn());
                    priceVo.setLast_price(priceThreshhold.getPrice_previous());
                    if (StringUtils.isNotEmpty(priceThreshhold.getPrice())) {
                        priceVo.setLatest_price(Double.valueOf(priceThreshhold.getPrice()));
                    }
                    if (priceThreshhold.getCreate_time() == null) {
                        priceThreshhold.setCreate_time(new Date());
                        pushTemplateEsBizHelper.saveEsBoomBustQueueVO(JSONObject.parseObject(JSONObject.toJSONString(priceThreshhold), BoomBustQueueVO.class));
                    }

                    priceVo.setCoinPriceMax(config.getCoinPriceMax());
                    priceVo.setCoinPriceMin(config.getCoinPriceMin());
                    priceVo.setBccName(config.getBccNameEn());
                    priceVo.setLast_price_time(DateUtils.formatDate(priceThreshhold.getCreate_time(), "yyyy/MM/dd HH:mm:ss"));
                    priceVo.setChange(priceThreshhold.getPrice_change_rate());
                }
                return new ResponseEntity(ResponseEnum.SUCCESS, priceVo);
        }
        log.error("【从es中获取微信模板信息，没有匹配到模板类型，参数 type: {}】", templateInfoFromEsBO.getType());
        return new ResponseEntity(ResponseEnum.FAIL, null);
    }

}
